﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TimeProject.Engine.DbModels;
using TimeProject.WebSite.ViewModels;
using TimeProject.Engine.DbContent;

namespace TimeProject.WebSite.Controllers.Actions
{
    public class UsersActionHandler : ActionHandlerBase
    {

        /// <summary>
        /// Gets the UsersModel for the current request.
        /// </summary>
        /// <returns>The UsersModel.</returns>
        public UsersViewModel GetUsersModel()
        {
            UsersViewModel viewModel = new UsersViewModel { Users = new List<UserViewModel>() };

            User[] users = Db.GetUsers();

            foreach (User user in users)
            {
                if (AllowView(user))
                {
                    UserViewModel um = ToUserModel(user);
                    viewModel.Users.Add(um);
                }
            }

            return viewModel;
        }

        /// <summary>
        /// Checks if the current user can view the data of specified user.
        /// </summary>
        /// <param name="user">The User.</param>
        /// <returns>true if the current user can view the data of the specified user, otherwise false.</returns>
        private bool AllowView(User user)
        {
            return true;
        }

        /// <summary>
        /// Creates a new UserModel from the specifies User.
        /// </summary>
        /// <param name="user">The User.</param>
        /// <returns>The UserModel.</returns>
        private UserViewModel ToUserModel(User user)
        {
            return new UserViewModel
            {
                UserId = user.Id,
                Username = user.Username,
                FullName = user.FullName,
                EMail = user.EMail
            };
        }

    }
}